# -*- mode: sh -*-

################################################################
### Environment

MY_GUIX_PROFILE_NAMES="main build misc guile games fonts emacs"

export MY_CONFIG_DIR="$HOME/config"
export MY_GUILE_DIR="$MY_CONFIG_DIR/guile"
export MY_GUIX_DIR="$MY_CONFIG_DIR/guix"
export MY_SHELL_DIR="$MY_CONFIG_DIR/shell"
export MY_PATH="$HOME/bin:$MY_GUILE_DIR/scripts:$MY_SHELL_DIR/scripts:$MY_CONFIG_DIR/guile-daemon/scripts"

export MY_GUIX_PROFILES_DIR="$HOME/.guix-profiles"
export MY_GUIX_USER_PROFILE_DIR="$MY_GUIX_PROFILES_DIR/main/main"
export MY_GUIX_SYSTEM_PROFILE_DIR="/run/current-system/profile"

add_dir_to_var_if_exists () {
    # Add directory $1 (if it exists) to environment variable $2.
    [ -d "$1" ] && eval $(echo "$2=$1:\$$2")
}

display_file_if_exists () {
    [ -a "$1" ] && echo "$1"
}

display_guix_profiles_paths () {
    for name in $MY_GUIX_PROFILE_NAMES
    do
        display_file_if_exists "$MY_GUIX_PROFILES_DIR/$name/$name"
    done
    display_file_if_exists "$MY_GUIX_SYSTEM_PROFILE_DIR"
}

set_guix_env () {
    # Calculate environment for all Guix profiles and add it to the
    # current environment.
    guix=$(PATH="$MY_PATH:$PATH" which guix)
    if [ $? -eq 0 ]; then   # if 'guix' executable is found
        env_cmd="$guix package --search-paths=prefix"
        for path in $(display_guix_profiles_paths)
        do
            env_cmd="$env_cmd --profile=$path"
            # XXX Currently XDG_... variables are not set by 'guix
            # package --search-paths' command, so handle them manually.
            add_dir_to_var_if_exists "$path/share" xdg_data_dirs
            add_dir_to_var_if_exists "$path/etc/xdg" xdg_config_dirs
        done
        export XDG_DATA_DIRS="$xdg_data_dirs$XDG_DATA_DIRS"
        export XDG_CONFIG_DIRS="$xdg_config_dirs$XDG_CONFIG_DIRS"
        echo "Running: $env_cmd"
        eval $($env_cmd)
    fi
}

locale_dir="$MY_GUIX_PROFILES_DIR/locales/locales/lib/locale"
[ -d "$locale_dir" ] && export GUIX_LOCPATH="$locale_dir"
export LANG=en_US.UTF-8

# Set some variables to default values if needed.
[ ! -v MANPATH ] && [ -d /usr/share/man ] && export MANPATH=/usr/share/man
[ ! -v XDG_DATA_DIRS ] && [ -d /usr/share ] && export XDG_DATA_DIRS=/usr/share
[ ! -v XDG_CONFIG_DIRS ] && [ -d /etc/xdg ] && export XDG_CONFIG_DIRS=/etc/xdg

set_guix_env

if [ -d $MY_GUIX_SYSTEM_PROFILE_DIR ]; then   # if it is GuixSD
    export PATH="/run/setuid-programs:$PATH"
fi

if [ -d $MY_GUIX_USER_PROFILE_DIR ]; then
    # Find GTK themes.
    export GTK_DATA_PREFIX="$MY_GUIX_USER_PROFILE_DIR"

    # Without this, other git outputs (like 'send-email') wouldn't work.
    export GIT_EXEC_PATH="$MY_GUIX_USER_PROFILE_DIR/libexec/git-core"

    aspell_dir="$MY_GUIX_USER_PROFILE_DIR/lib/aspell"
    if [ -d $aspell_dir ]; then
        # export ASPELL_CONF="data-dir $aspell_dir; dict-dir $aspell_dir"
        export ASPELL_CONF="dict-dir $aspell_dir"
    fi
fi

certs_dir=/var/guix/profiles/system/profile/etc/ssl/certs
if [ -d "$certs_dir" ]; then
    export SSL_CERT_DIR="$certs_dir"
    export SSL_CERT_FILE="$certs_dir/ca-certificates.crt"
    export GIT_SSL_CAINFO="$SSL_CERT_FILE"
fi

export PATH="$MY_PATH:$PATH"
export SSH_AUTH_SOCK="$HOME/.gnupg/S.gpg-agent.ssh"
export GUIX_PACKAGE_PATH="$MY_GUIX_DIR/guix-package-path"

misc_guile_dirs="$MY_GUILE_DIR/modules:$MY_GUIX_DIR/modules:$HOME/devel/guix:$HOME/devel/shepherd/modules"
export GUILE_LOAD_PATH="$misc_guile_dirs${GUILE_LOAD_PATH:+:}$GUILE_LOAD_PATH"
export GUILE_LOAD_COMPILED_PATH="$misc_guile_dirs${GUILE_LOAD_COMPILED_PATH:+:}$GUILE_LOAD_COMPILED_PATH"

export EDITOR='emacsclient'
export VIEWER='emacsclient'

export XDG_CONFIG_HOME="$HOME/.config"
export XDG_CACHE_HOME="$HOME/.cache"
export XDG_DATA_HOME="$HOME/.local/share"

# Always show normal scrollbars.
export GTK_OVERLAY_SCROLLING=0

# Prevent sox from using pulseaudio.
export AUDIODRIVER=alsa
# Without this, there is no sound in programs (usually games) that use
# sdl-mixer (at least on GuixSD).
export SDL_AUDIODRIVER=alsa

################################################################

[[ -f ~/.bashrc ]] && . ~/.bashrc

# Start shepherd if it is not already running.
[[ -z $(pgrep -U $(id --user) "^shepherd$") ]] \
    && shepherd &>> $HOME/.config/shepherd/shepherd.log
